/**
 * 导航工具类
 */

// 团购管理相关页面路由
export const GrouponRoutes = {
  // 团购首页
  INDEX: '/pages/groupons/index',
  
  // 团购活动
  ACTIVITY_LIST: '/pages/groupons/activity/index',
  ACTIVITY_DETAIL: '/pages/groupons/activity/detail',
  ACTIVITY_CREATE: '/pages/groupons/activity/detail?mode=create',
  
  // 订单管理
  ORDER_LIST: '/pages/groupons/order/index',
  ORDER_DETAIL: '/pages/groupons/order/detail',
  
  // 数据统计
  STATISTICS: '/pages/groupons/statistics/index',
  
  // 核销管理
  VERIFY_LIST: '/pages/groupons/verify/index',
  VERIFY_NEW: '/pages/groupons/verify/new',
  VERIFY_DETAIL: '/pages/groupons/verify/detail',
  VERIFY_SCAN: '/pages/groupons/verify/new?type=scan',
  VERIFY_MANUAL: '/pages/groupons/verify/new?type=manual',
};

// 导航方法
export class Navigation {
  /**
   * 跳转到团购首页
   */
  static toGrouponIndex() {
    uni.switchTab({
      url: GrouponRoutes.INDEX,
    });
  }

  /**
   * 跳转到团购活动列表
   */
  static toActivityList() {
    uni.navigateTo({
      url: GrouponRoutes.ACTIVITY_LIST,
    });
  }

  /**
   * 跳转到团购活动详情
   * @param id 活动ID
   * @param mode 模式：view查看、edit编辑、create创建
   */
  static toActivityDetail(id?: number, mode: 'view' | 'edit' | 'create' = 'view') {
    let url = GrouponRoutes.ACTIVITY_DETAIL + `?mode=${mode}`;
    if (id) {
      url += `&id=${id}`;
    }
    uni.navigateTo({ url });
  }

  /**
   * 创建团购活动
   */
  static toActivityCreate() {
    uni.navigateTo({
      url: GrouponRoutes.ACTIVITY_CREATE,
    });
  }

  /**
   * 编辑团购活动
   * @param id 活动ID
   */
  static toActivityEdit(id: number) {
    uni.navigateTo({
      url: `${GrouponRoutes.ACTIVITY_DETAIL}?mode=edit&id=${id}`,
    });
  }

  /**
   * 查看团购活动详情
   * @param id 活动ID
   */
  static toActivityView(id: number) {
    uni.navigateTo({
      url: `${GrouponRoutes.ACTIVITY_DETAIL}?mode=view&id=${id}`,
    });
  }

  /**
   * 跳转到订单管理列表
   */
  static toOrderList() {
    uni.navigateTo({
      url: GrouponRoutes.ORDER_LIST,
    });
  }

  /**
   * 跳转到订单详情
   * @param id 订单ID
   */
  static toOrderDetail(id: number) {
    uni.navigateTo({
      url: `${GrouponRoutes.ORDER_DETAIL}?id=${id}`,
    });
  }

  /**
   * 跳转到数据统计页面
   */
  static toStatistics() {
    uni.navigateTo({
      url: GrouponRoutes.STATISTICS,
    });
  }

  /**
   * 跳转到核销管理列表
   */
  static toVerifyList() {
    uni.navigateTo({
      url: GrouponRoutes.VERIFY_LIST,
    });
  }

  /**
   * 跳转到核销详情
   * @param id 核销记录ID
   */
  static toVerifyDetail(id: number) {
    uni.navigateTo({
      url: `${GrouponRoutes.VERIFY_DETAIL}?id=${id}`,
    });
  }

  /**
   * 跳转到扫码核销
   */
  static toVerifyScan() {
    uni.navigateTo({
      url: GrouponRoutes.VERIFY_SCAN,
    });
  }

  /**
   * 跳转到手动核销
   */
  static toVerifyManual() {
    uni.navigateTo({
      url: GrouponRoutes.VERIFY_MANUAL,
    });
  }

  /**
   * 返回上一页
   * @param delta 返回层数
   */
  static goBack(delta: number = 1) {
    uni.navigateBack({ delta });
  }

  /**
   * 重定向到指定页面
   * @param url 页面路径
   */
  static redirectTo(url: string) {
    uni.redirectTo({ url });
  }

  /**
   * 关闭所有页面，打开到应用内的某个页面
   * @param url 页面路径
   */
  static reLaunch(url: string) {
    uni.reLaunch({ url });
  }

  /**
   * 跳转到 tabBar 页面
   * @param url 页面路径
   */
  static switchTab(url: string) {
    uni.switchTab({ url });
  }

  /**
   * 显示消息提示框
   * @param title 提示内容
   * @param icon 图标类型
   * @param duration 显示时长
   */
  static showToast(title: string, icon: 'success' | 'error' | 'loading' | 'none' = 'none', duration: number = 2000) {
    uni.showToast({
      title,
      icon,
      duration,
    });
  }

  /**
   * 显示模态对话框
   * @param options 对话框选项
   */
  static showModal(options: {
    title?: string;
    content?: string;
    showCancel?: boolean;
    cancelText?: string;
    confirmText?: string;
  }): Promise<boolean> {
    return new Promise((resolve) => {
      uni.showModal({
        title: options.title || '提示',
        content: options.content || '',
        showCancel: options.showCancel !== false,
        cancelText: options.cancelText || '取消',
        confirmText: options.confirmText || '确定',
        success: (res: any) => {
          resolve(res.confirm);
        }
      });
    });
  }

  /**
   * 显示操作菜单
   * @param itemList 菜单项列表
   */
  static showActionSheet(itemList: string[]): Promise<number> {
    return new Promise((resolve, reject) => {
      uni.showActionSheet({
        itemList,
        success: (res: any) => {
          resolve(res.tapIndex);
        },
        fail: (err: any) => {
          reject(err);
        }
      });
    });
  }

  /**
   * 设置导航栏标题
   * @param title 标题文本
   */
  static setNavigationBarTitle(title: string) {
    uni.setNavigationBarTitle({ title });
  }

  /**
   * 显示导航栏加载动画
   */
  static showNavigationBarLoading() {
    uni.showNavigationBarLoading();
  }

  /**
   * 隐藏导航栏加载动画
   */
  static hideNavigationBarLoading() {
    uni.hideNavigationBarLoading();
  }

  /**
   * 获取当前页面栈
   */
  static getCurrentPages() {
    return getCurrentPages();
  }

  /**
   * 获取当前页面路径
   */
  static getCurrentRoute(): string {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    return currentPage.route || '';
  }

  /**
   * 获取当前页面参数
   */
  static getCurrentOptions(): Record<string, any> {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1] as any;
    return currentPage.options || {};
  }

  /**
   * 判断是否为指定页面
   * @param route 页面路径
   */
  static isCurrentPage(route: string): boolean {
    return this.getCurrentRoute() === route.replace(/^\//, '');
  }

  /**
   * 预加载页面
   * @param url 页面路径
   */
  static preloadPage(url: string) {
    uni.preloadPage({ url });
  }
}

// 导出常用的导航方法
export const {
  toGrouponIndex,
  toActivityList,
  toActivityDetail,
  toActivityCreate,
  toActivityEdit,
  toActivityView,
  toOrderList,
  toOrderDetail,
  toStatistics,
  toVerifyList,
  toVerifyDetail,
  toVerifyScan,
  toVerifyManual,
  goBack,
  showToast,
  showModal,
  showActionSheet,
} = Navigation;

// 默认导出
export default Navigation;